import java.util.Currency;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-03-26
 * Time: 8:26
 */
public class Test1 {

    public static void init(int[] arr) {
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100) + 1;
        }
    }

    public static int count = 0;
    public static Object object = new Object();



    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized(object) {
                for (int i = 0; i < 1_0000; i++) {
                    count++;
                }
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized(object) {
                for (int i = 0; i < 1_0000; i++) {
                    count++;
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(count);
    }



    public static void main1(String[] args) throws InterruptedException {
        long time1 = System.currentTimeMillis();
        int[] arr = new int[1000_0000];
        init(arr);
        Thread t1 = new Thread(() -> {
            synchronized (object) {
                for (int i = 0; i < arr.length; i += 2) {
                    count += arr[i];
                }
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (object) {
                for (int i = 1; i < arr.length; i += 2) {
                    count += arr[i];
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(count);
        System.out.println(System.currentTimeMillis() - time1);
    }
}
